package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Menu;
import cn.wolfcode.domain.SystemDictionary;
import cn.wolfcode.mapper.SystemDictionaryMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.qo.SystemDictionaryQueryObject;
import cn.wolfcode.service.ISystemDictionaryService;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SystemDictionaryServiceImpl implements ISystemDictionaryService {
    private SystemDictionaryMapper systemDictionaryMapper ;

    @Autowired
    public void setSystemDictionaryMapper(SystemDictionaryMapper systemDictionaryMapper) {
        this.systemDictionaryMapper = systemDictionaryMapper;
    }

    @Override
    public void insert(SystemDictionary systemDictionary) {
        systemDictionaryMapper.insert(systemDictionary);
    }

    @Override
    public void delete(Long id) {
        systemDictionaryMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(SystemDictionary systemDictionary) {
        systemDictionaryMapper.updateByPrimaryKey(systemDictionary);
    }

    @Override
    public SystemDictionary selectById(Long id) {
        SystemDictionary systemDictionary = systemDictionaryMapper.selectByPrimaryKey(id);
        return systemDictionary ;
    }

    @Override
    public List<SystemDictionary> selectAll() {
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.selectAll();
        return systemDictionarys ;
    }

    @Override
    public PageInfo<SystemDictionary> query(SystemDictionaryQueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<SystemDictionary> systemDictionarys = systemDictionaryMapper.queryAll(qo);
        return new PageInfo<>(systemDictionarys);
    }


    @Override
    public List<SystemDictionary> selectByBs(String business) {
        List<SystemDictionary> systemDictionaries = systemDictionaryMapper.selectByBs(business);
        return systemDictionaries;
    }

    @Override
    public List<SystemDictionary> queryByParentId(Long parentId) {
        return systemDictionaryMapper.selectByParentId(parentId);
    }


    @Override
    public List<Map> queryTreeData() {
        List<SystemDictionary> all = this.selectAll();
        List<SystemDictionary> top = new ArrayList<>();
        for (SystemDictionary item : all) {
            if(item.getParentId() == null){
                top.add(item);
                //可不要
//                item.setChildren(this.findChildren(all, item.getId()));
            }
        }
        fillChildren(all, top);

        String json =  JSON.toJSONString(top);
        json = json.replaceAll("name", "text")
                .replaceAll("children", "nodes");
        List<Map> mapList = JSON.parseArray(json, Map.class);

        return mapList;
    }


    private List<SystemDictionary> findChildren(List<SystemDictionary> all, Long parentId){
        List<SystemDictionary> children = new ArrayList<>();
        for (SystemDictionary item : all) {
            if(item.getParentId() == parentId){
                children.add(item);
            }
        }
        return children.size()>0?children:null;
    }
    private void fillChildren(List<SystemDictionary> all, List<SystemDictionary> children){

        if(children == null){
            return;
        }
        for (SystemDictionary item : children) {
            item.setChildren(findChildren(all, item.getId()));
            fillChildren(all, item.getChildren());
        }
    }
}
